<?php

namespace app\admin\controller;


class Banner extends Base
{
    //轮播图展示
    public function bannerlist()
    {
        $banner = model('common/Banner')
            -> order('create_time','desc')
            -> paginate(10);
        $data = [
            'banner' => $banner
        ];
        $this -> assign($data);
        return view();
    }

    //轮播图添加
    public function banner_add()
    {
        if (Request()->isAjax()){
            $file = request()->file('myFile');
            $result = model('common/Banner') -> banner_add($file);
            if ($result == 1){
                $this -> success('添加成功！','admin/banner/bannerlist');
            } else {
                $this -> error($result);
            }
        }
        return view('banner/banneradd');
    }

    //轮播图状态控住
    public function banner_status()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $result = model('common/Banner') -> banner_status($data);
            if ($result == 1){
                $this -> success('操作成功！','admin/banner/bannerlist');
            } else {
                $this -> error($result);
            }
        }
    }

    //banner图删除
    public function banner_del()
    {
        $data = model('common/Banner') -> find(input('post.id'));
        $result = $data -> delete();
        if ($result){
            $this -> success('操作成功！','admin/banner/bannerlist');
        } else {
            $this -> error('操作失败！');
        }
    }

    //banner编辑
    public function banner_edit()
    {
        if (request()->isAjax()){
            $id = input('post.id');
            $file = request() -> file('myFile');
            $data = [
                'id' => $id,
                'file' => $file
            ];
            $result = model('common/Banner') -> banner_edit($data);
            if ($result){
                $this -> success('操作成功！','admin/banner/bannerlist');
            } else {
                $this -> error('操作失败！');
            }
        }
        $banner = model('common/Banner') -> find(input('id'));
        $data = [
            'banner' => $banner
        ];
        $this -> assign($data);
        return view('banner/banneredit');
    }


}
